﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Salon
{
    public partial class FormCar : Form
    {
        public Car EditingCar { get; set; }

        public FormCar()
        {
            InitializeComponent();
            Text = Forms.CAR_FORM;

        }
        //konstruktor inicjujący formatkę danymi konkretnego klienta
        public FormCar(Car car)
        {
            InitializeComponent();
            if (car != null)
            {
                //fillFields(car); - musi być wypełnione po inicjelizaciji pól
                this.EditingCar = car;
            }

            Text = Forms.CAR_FORM;
        }

        private void fillFields(Car car)
        {
            this.EditingCar = car;
            this.txtId.Text = car.Id.ToString() ;
            this.txtYear.Text = car.Year.ToString();
            this.cmbColor.SelectedValue = car.Color;
            this.cmbAutoSalon.SelectedValue = car.Salon.Id;
            this.cmbMark.SelectedValue = car.Mark.Id;
            markComboBox_SelectedIndexChanged(null,null);
            this.cmbModel.SelectedValue = car.Model.Id;

            this.txtCost.Text = car.Cost.ToString();
            this.txtCost.Enabled = false;
            this.txtPrice.Text = car.Price.ToString();
            this.txtPrice.Enabled = false;
            this.txtDiscount.Text = car.Discount.ToString();
        }

        private void FormCar_Load(object sender, EventArgs e)
        {
            cmbMark.Initialize();
            //cmbModel.Initialize();
            cmbModel.Enabled = false;
            cmbColor.Initialize();
            cmbAutoSalon.Initialize();

            if (this.EditingCar != null) fillFields(this.EditingCar);

            cmbMark.SelectedIndexChanged += markComboBox_SelectedIndexChanged;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Global.MainPage.openNewCarForm(null);
            DictColor ff = new DictColor();
            ff.MdiParent = Global.MainPage;
            ff.Show();
        }

        private void markComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Aktualizacja modelu elementami z danej marki
            if (cmbMark.SelectedValue != null)
            {
                cmbModel.InitializeMark((int)cmbMark.SelectedValue);
                cmbModel.Enabled = true;
                cmbModel.SelectedItem = null;
            }
            else
            {
                cmbModel.Enabled = false;
                cmbModel.SelectedItem = null;
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            this.EditingCar.Id = 0;
            this.txtId.Text = "";
            this.txtYear.Text = "";
            this.txtCost.Text = "";
            this.txtPrice.Text = "";
            this.txtDiscount.Text = "";
            this.cmbMark.SelectedItem = null;
            this.cmbModel.SelectedItem = null;
            this.cmbColor.SelectedItem = null;
            this.cmbAutoSalon.SelectedItem = null;
            //this.lchbOptions.ClearSelected();
            //this.lchbMountedOptions.ClearSelected();
            this.txtCost.Enabled = true;
            this.txtPrice.Enabled = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private String validateFields()
        {
            String fields = "";
            fields += cmbAutoSalon.SelectedValue == null ? "salon, " : "";
            fields += cmbColor.SelectedValue == null ? "kolor, " : "";
            fields += cmbMark.SelectedValue == null ? "markę, " : "";
            fields += cmbModel.SelectedValue == null ? "model, " : "";
            fields += txtYear.Text.Equals(String.Empty) ? "rok, " : "";
            fields += txtCost.Text.Equals(String.Empty) ? "koszt, " : "";
            fields += txtPrice.Text.Equals(String.Empty) ? "cenę, " : "";
            fields += txtDiscount.Text.Equals(String.Empty) ? "rabat, " : "";
            
            return fields;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            String fields = validateFields();
            bool nowe = ( EditingCar == null );

            if (!fields.Equals(String.Empty))
            {
                MessageBox.Show("Proszę uzupełnić pola " + fields.Substring(0, fields.Length - 2) + ".");
                return;
            }

            Car car = nowe ? new Car() : this.EditingCar;
            car.Mark = ServiceFactory.getDictService().getMarkById((int)this.cmbMark.SelectedValue);
            car.Model = ServiceFactory.getDictService().getModelById((int)this.cmbModel.SelectedValue);
            car.Color = (int)this.cmbColor.SelectedValue;
            car.Year = Convert.ToInt32(this.txtYear.Text);
            car.Salon = ServiceFactory.getAutoSalonService().getAutoSalonById((int)this.cmbAutoSalon.SelectedValue);
            car.Cost = (float)Convert.ToDouble(this.txtCost.Text);
            car.Price = (float)Convert.ToDouble(this.txtPrice.Text);
            car.Discount = (float)Convert.ToDouble(this.txtDiscount.Text);
            
            car = ServiceFactory.getCarService().saveCar(car);


            // ... dopisanie do samochodu opcji z modelu ...

            // pobranie opcji modelu
            IList<ModelOptionItem> moiList = ServiceFactory.getDictService().getModelOptions( car.Model.Id );
            IList<CarOptionItem> coiList = new List<CarOptionItem>();
            foreach( ModelOptionItem moi in moiList )
            if( moi.Active )
            {
                CarOptionItem coi = new CarOptionItem();
                coi.Cost = (float)moi.Cost;
                coi.Price = (float)moi.Price;
                coi.Discount = (float)moi.Discount;
                coi.Active = true; //coiList.Active = moi.Active;
                coi.Id = moi.Id;
                coi.MountDate = DateTime.Now; // ? przecieżnie jest zamontowany
                coi.ServicemanId = 0;
                coi.State = (CarOptionItem.EnumState) moi.Status; // ( !? )
                coiList.Add( coi );
            }
            if(nowe) ServiceFactory.getDictService().addCarOptions(car.Id, coiList);
            // else UPDATE

        }



    }
}
